String Functions


Exercise 1

Create a function which takes string and a character as arguments and
return a string by removing similar characters from string.
let myStrFunc ("Hello World", "o") //output : "Hell Wrld"

   
function myFunc1(str, a) {
  return str.replaceAll(a, "");
}
document.getElementById("ex1").innerText = myFunc1("Hello World", "o");

Exercise 2

Create a function which takes string as argument and return the count
of Blank Space in a string.
let strl = "TypeScript is better than JavaScript"; //Output : 4
let str2 = "Dcoders"; //Output : O
let str3 = "Learning JavaScript is a fun and enjoyable"; //Output : 6

   
function myFunc2(str) {
  return str.split(" ").length - 1;
}
document.getElementById("ex2").innerText = myFunc2(
  "this Text Contain Four Spaces"
);

Exercise 3

Create a function which takes 2 strings as an argument and return
Joined string as example.
let newStr("HeIIo", "Coders") //Output : "Coders Hello"
let newStr("Hi", "India");
//Output . "India Hi"

   

function myFunc3(str1, str2) {
  return str2 + " " + str1;
}
document.getElementById("ex3").innerText = myFunc3("Hello", "Coders");

Exercise 4

Create a function which takes string as argument and return the all the
small "a" to Capital "A".
let Str= "I am learning Javascript"; //I Am IeArning jAvAscript;
let str2 = "TypeScript"; //TypeScript;

   

function myFunc4(str) {
  return str.replaceAll("a", "A");
}
document.getElementById("ex4").innerText = myFunc4("I am learning javascript");

Exercise 5

Create a functions which takes String as argument return string by
removing last character.
let removeLast("Kites") //Output :kite
let removeLast("GIobaI") //Output :"Global'

   

function myFunc5(str) {
  return str.slice(0,-1);
}
document.getElementById("ex5").innerText = myFunc5("javascript");

  

Exercise 6

Create a functions which takes an String (includes numbers) and extract a
numbers from a string.
let extractNum("Docders15Lab"); //Output : 15
let extractNum("India555") //Output : 555

   
function myFunc6(str) {
    let numStr = "";
  for (let i = 0; i < str.length; i++) {
    if (!isNaN(str[i]) && str[i] !== " ") {
      numStr += str[i];
    }
  }
  return Number(numStr);
}
document.getElementById("ex6").innerText = myFunc6("javascript2ai3ssdqdq2131cd");


Exercise 7

Create a functions which takes String as argument return new string with
all characters in upper case.
let changeToUpper("india")//Output : "INDIA"
let changeToUpper("dcoders")//Output : "DCODERS"

   
function myFunc7(str) {
  return str.toUpperCase();
}
document.getElementById("ex7").innerText = myFunc7("javascript");

Exercise 8

Create a functions which take 2 String numbers and return a sum of them in
number datatype.
let getSum("20", "50"); //Output : 70
let getSum("15", "15") //Output : 30

   

function myFunc8(a,b) {
return +a  +  +b;
}
document.getElementById("ex8").innerText = myFunc8(30,40);


Exercise 9

Create a functions which takes String and a character as arguments return
boolean value if String has that character at start.
let checkChar("JavaScript", "J") //Output : true
let checkChar("dcoders", "p") //Output : false

   

function myFunc9(str,a) {
 return str.startsWith(a)
}
document.getElementById("ex9").innerText = myFunc9("JAVA","j");

Exercise 10

Create a functions which take String and Character return the position of Character from string.
let extractNum("HeIIo", "e"); //Output : 2
let extractNum("TypeScript", "S") //Output : 5

   
function myFunc10(str,a) {
  let location = str.indexOf(a) + 1;
  if(location <= 0){
    return "Not Exist"
  }
  return location;
}
document.getElementById("ex10").innerText = myFunc10("JAVA","J");